package com.yyjj.zixun.server;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.vise.xsnow.http.ViseHttp;
import com.vise.xsnow.http.callback.ACallback;
import com.vise.xsnow.http.request.GetRequest;
import com.yyjj.zixun.entity.ArticleBean;
import com.yyjj.zixun.entity.ArticleDetailsBean;
import com.yyjj.zixun.entity.CalendarBean;
import com.yyjj.zixun.entity.CurrencyArticleBean;
import com.yyjj.zixun.entity.CurrencyArticleDetailsBean;
import com.yyjj.zixun.entity.CurrencyNewsletterBean;
import com.yyjj.zixun.entity.NewsletterBean;
import com.yyjj.zixun.entity.VideoBean;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import tech.com.commoncore.base.BaseApplication;
import tech.com.commoncore.constant.ApiConstant;

public class NewsServerImp implements NewsServer {
    private static NewsServerImp newsServerImp;

    public static NewsServerImp getInstance() {
        synchronized (NewsServer.class) {
            if (newsServerImp == null)
                NewsServerImp.newsServerImp = new NewsServerImp();
            return newsServerImp;
        }
    }

    @Override
    public void requestNewsletter(int type, int size, final NewsBack newsBack) {
        ViseHttp.GET("/new_api/Newsletter/index/").baseUrl(ApiConstant.BASE_URL)
                .addParam("type", String.valueOf(type))
                .addParam("pagesize", String.valueOf(size))
                .request(new ACallback<String>() {
                    @Override
                    public void onSuccess(String data) {
                        JSONObject jsonObject = JSONObject.parseObject(data);
                        if (jsonObject.getIntValue("code") == 1) {
                            List<NewsletterBean> list = JSONArray.parseArray(jsonObject.getString("data"), NewsletterBean.class);
                            handlerNewsBack(newsBack, list, null);
                        } else {
                            handlerNewsBack(newsBack, null, jsonObject.getString("msg"));
                        }
                    }

                    @Override
                    public void onFail(int errCode, String errMsg) {
                        handlerNewsBack(newsBack, null, errMsg);
                    }
                });
    }

    @Override
    public void requestCurrencyNewsletter(final NewsBack newsBack) {
        ViseHttp.GET("/yapi/Currency/newsletter/").baseUrl(ApiConstant.BASE_URL)
                .request(new ACallback<String>() {
                    @Override
                    public void onSuccess(String data) {
                        JSONObject jsonObject = JSONObject.parseObject(data);
                        if (jsonObject.getIntValue("code") == 1) {
                            JSONObject dataJson = jsonObject.getJSONObject("data");
                            List<CurrencyNewsletterBean> list = new ArrayList<>();
                            for (Map.Entry<String, Object> entry : dataJson.entrySet()) {
                                CurrencyNewsletterBean bean = JSONObject.parseObject(dataJson.getString(entry.getKey()), CurrencyNewsletterBean.class);
                                list.add(bean);
                            }
                            handlerNewsBack(newsBack, list, null);
                        } else {
                            handlerNewsBack(newsBack, null, jsonObject.getString("msg"));
                        }
                    }

                    @Override
                    public void onFail(int errCode, String errMsg) {
                        handlerNewsBack(newsBack, null, errMsg);
                    }
                });
    }

    @Override
    public void requestArticle(int page, int size, String order, String category, final NewsBack newsBack) {
        GetRequest request = ViseHttp.GET("/new_api/News/index/").baseUrl(ApiConstant.BASE_URL)
                .addParam("page", String.valueOf(page))
                .addParam("pagesize", String.valueOf(size))
                .addParam("order", order)
                .addParam("category", category);
        request.request(new ACallback<String>() {
            @Override
            public void onSuccess(String data) {
                JSONObject jsonObject = JSONObject.parseObject(data);
                if (jsonObject.getIntValue("code") == 1) {
                    JSONObject value = jsonObject.getJSONObject("data");
                    List<ArticleBean> list = JSONArray.parseArray(value.getString("data"), ArticleBean.class);
                    handlerNewsBack(newsBack, list, null);
                } else {
                    handlerNewsBack(newsBack, null, jsonObject.getString("msg"));
                }
            }

            @Override
            public void onFail(int errCode, String errMsg) {
                handlerNewsBack(newsBack, null, errMsg);
            }
        });
    }

    @Override
    public void requestCurrencyArticle(int type, int page, final NewsBack newsBack) {
        ViseHttp.GET("/yapi/Currency/btctrade_list/").baseUrl(ApiConstant.BASE_URL)
                .addParam("category", String.valueOf(type))
                .addParam("page", String.valueOf(page))
                .request(new ACallback<String>() {
                    @Override
                    public void onSuccess(String data) {
                        JSONObject jsonObject = JSONObject.parseObject(data);
                        if (jsonObject.getIntValue("code") == 1) {
                            List<CurrencyArticleBean> list = JSONArray.parseArray(jsonObject.getString("data"), CurrencyArticleBean.class);
                            handlerNewsBack(newsBack, list, null);
                        } else {
                            handlerNewsBack(newsBack, null, jsonObject.getString("msg"));
                        }
                    }

                    @Override
                    public void onFail(int errCode, String errMsg) {
                        handlerNewsBack(newsBack, null, errMsg);
                    }
                });
    }

    @Override
    public void requestArticleDetails(int articleId, final NewsBack newsBack) {
        ViseHttp.GET("/new_api/News/view/").baseUrl(ApiConstant.BASE_URL)
                .addParam("id", String.valueOf(articleId))
                .request(new ACallback<String>() {
                    @Override
                    public void onSuccess(String data) {
                        JSONObject jsonObject = JSONObject.parseObject(data);
                        if (jsonObject.getIntValue("code") == 1) {
                            ArticleDetailsBean bean = JSONObject.parseObject(jsonObject.getString("data"), ArticleDetailsBean.class);
                            handlerNewsBack(newsBack, bean, null);
                        } else {
                            handlerNewsBack(newsBack, null, jsonObject.getString("msg"));
                        }
                    }

                    @Override
                    public void onFail(int errCode, String errMsg) {
                        handlerNewsBack(newsBack, null, errMsg);
                    }
                });
    }

    @Override
    public void requestCurrencyArticleDetails(String articleId, final NewsBack newsBack) {
        ViseHttp.GET("/yapi/Currency/btctrade_view/").baseUrl(ApiConstant.BASE_URL)
                .addParam("id", articleId)
                .request(new ACallback<String>() {
                    @Override
                    public void onSuccess(String data) {
                        JSONObject jsonObject = JSONObject.parseObject(data);
                        if (jsonObject.getIntValue("code") == 1) {
                            CurrencyArticleDetailsBean bean = JSONObject.parseObject(jsonObject.getString("data"), CurrencyArticleDetailsBean.class);
                            handlerNewsBack(newsBack, bean, null);
                        } else {
                            handlerNewsBack(newsBack, null, jsonObject.getString("msg"));
                        }
                    }

                    @Override
                    public void onFail(int errCode, String errMsg) {
                        handlerNewsBack(newsBack, null, "网络错误");
                    }
                });
    }

    @Override
    public void requestCalendarNews(int year, int month, int day, int importance, String effect, final NewsBack newsBack) {
        ViseHttp.GET(ApiConstant.CJRL).baseUrl(ApiConstant.BASE_URL)
                .addParam("year", String.valueOf(year))
                .addParam("monthday", String.valueOf(month) + String.valueOf(day))
                .addParam("importance", String.valueOf(importance))
                .addParam("effect", String.valueOf(effect))
                .request(new ACallback<String>() {
                    @Override
                    public void onSuccess(String data) {
                        JSONObject jsonObject = JSONObject.parseObject(data);
                        if (jsonObject.getIntValue("code") == 1) {
                            List<CalendarBean> bean = JSONArray.parseArray(jsonObject.getString("data"), CalendarBean.class);
                            handlerNewsBack(newsBack, bean, null);
                        } else {
                            handlerNewsBack(newsBack, null, jsonObject.getString("msg"));
                        }
                    }

                    @Override
                    public void onFail(int errCode, String errMsg) {
                        handlerNewsBack(newsBack, null, "网络错误");
                    }
                });
    }

    @Override
    public void requestVideo(int type, int size, NewsBack newsBack) {
        String json = AssetsUtil.getJson("video.json", BaseApplication.getInstance());
        List<VideoBean> videoBeans = JSONArray.parseArray(json, VideoBean.class);
        if (size <= 0 || size >= videoBeans.size()) {
            handlerNewsBack(newsBack, videoBeans, null);
        } else {
            handlerNewsBack(newsBack, videoBeans.subList(0, size), null);
        }
    }

    private synchronized void handlerNewsBack(NewsBack back, Object object, String err) {
        if (back != null) {
            back.onBack(object, err);
        }
    }
}
